package com.view.houseon;

import java.util.ArrayList;


import com.controller.houseon.ControlHouseON;
import com.model.houseon.CasaBean;
import com.presentacion.houseon.R;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;


import android.view.ContextMenu;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.AdapterView;


import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;



public class ListaCasaActivity extends Activity   {

	GridView lstCasas;
	ArrayList<CasaBean> lista;
	TextView nombres;
	TextView correo;
	int band=0;
	CasaBean d;
	ImageButton ibtn_profile;
	ImageButton ibtn_information;
	ImageButton ibtn_logout;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_lista_casa);
		
		ibtn_logout=(ImageButton)findViewById(R.id.ibtnEscenarios);
		ibtn_logout.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//Toast.makeText(ListaCasaActivity.this, String.valueOf("logout.."), Toast.LENGTH_SHORT).show();
			    AlertDialog.Builder alert = new AlertDialog.Builder(ListaCasaActivity.this);
                alert.setTitle("Desea Salir?");
                alert.setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {  
                        salir();
                        }});
                           alert.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {  
                               public void onClick(DialogInterface dialog, int whichButton) {    
                            }});
                       alert.show();
			}
		});
		
		ibtn_profile=(ImageButton)findViewById(R.id.ibtnHome);
		ibtn_profile.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//Toast.makeText(ListaCasaActivity.this, String.valueOf("perfil.."), Toast.LENGTH_SHORT).show();
				 Intent i = new Intent(ListaCasaActivity.this, PerfilUsuarioActivity.class);
				 startActivity(i);
			}
		});
		

		ibtn_information=(ImageButton)findViewById(R.id.ibtnInformacion);
		ibtn_information.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//Toast.makeText(ListaCasaActivity.this, String.valueOf("info.."), Toast.LENGTH_SHORT).show();
				 Intent i = new Intent(ListaCasaActivity.this, InformacionHouseONActivity.class);
				 startActivity(i);
			}
		} );
		
	
		
		lstCasas =(GridView)findViewById(R.id.gvCasas);
		nombres= (TextView) findViewById(R.id.txt_nombre_user);
		//correo= (TextView) findViewById(R.id.txtCorreo_user);
		nombres.setText(ControlHouseON.getInstancia().CurrentUsuario.getNombres());
		//correo.setText(ControlHouseON.getInstancia().CurrentUsuario.getEmail());
		
		lista=ControlHouseON.getInstancia().ListaCasa(ControlHouseON.getInstancia().CurrentUsuario.getIdUsuario());
		
		ItemCasaAdapter item=new ItemCasaAdapter(ListaCasaActivity.this, lista);
		lstCasas.setAdapter(item);
		
		lstCasas.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> l, View v, int position,
					long id) {
				//Toast.makeText(ListaCasaActivity.this, String.valueOf("id: "+ id), Toast.LENGTH_SHORT).show();
				// TODO Auto-generated method stub
				Intent i = new Intent(ListaCasaActivity.this,CategoriasCasaActivity.class);
				ControlHouseON.getInstancia().CurrentCasa= new CasaBean();
				ControlHouseON.getInstancia().CurrentCasa.setIdCasa((int)id);
				startActivity(i);
			}
		});
	}
	
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.lista_casa, menu);
		return true;
	}
	
	 @Override
	    public boolean onOptionsItemSelected(MenuItem item) {
	        switch (item.getItemId()) {
	        
	         case R.id.action_salir:
	                AlertDialog.Builder alert = new AlertDialog.Builder(this);
	                alert.setTitle("Desea Salir?");
	                alert.setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
	                    public void onClick(DialogInterface dialog, int whichButton) {  
	                        salir();
	                        }});
	                           alert.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {  
	                               public void onClick(DialogInterface dialog, int whichButton) {    
	                            }});
	                       alert.show();
	             
	            return true;

	        default:
	            return super.onOptionsItemSelected(item);
	        }
	    }
	    
	
   @Override
   public boolean onKeyDown(int keyCode, KeyEvent event) {
	   return false;
   }
   
   public  void salir(){
       Intent i = new Intent(); 
                  i.setAction( Intent.ACTION_MAIN); 
                  i.addCategory( Intent.CATEGORY_HOME); 
                  i.setFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP); 
                  startActivity(i); 
                  android.os.Process.killProcess( android.os.Process.myPid()); 
                  finish();
  }
	
	public class ItemCasaAdapter extends BaseAdapter{
		  protected Activity activity;
		  protected ArrayList<CasaBean> items;

		 ImageButton ibtn_editar;
		 ImageButton ibtn_info;
		 View layout;
		 AlertDialog.Builder builder;
		 EditText nombre;
		 TextView txt_nombre;
		 
		 
		  public ItemCasaAdapter(Activity activity, ArrayList<CasaBean> items) {
		    this.activity = activity;
		    this.items = items;
		  }
		 
		  public int getCount() {
		    return items.size();
		  }
		 
		  public Object getItem(int position) {
		    return items.get(position);
		    
		  }
		 
		  public long getItemId(int position) {
		    return items.get(position).getIdCasa();
		  }
		 
		  public View getView(int position, View convertView, ViewGroup parent) {
		    View vi=convertView;
		    //this.position=position;
		    if(convertView == null) {
		      LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		      vi = inflater.inflate(R.layout.item_casa, null);
		      
		      ibtn_editar=(ImageButton)vi.findViewById(R.id.ibtnEdit);
		      ibtn_info=(ImageButton)vi.findViewById(R.id.ibtn_info);
		      
		      ibtn_info.setFocusable(false);
		      ibtn_info.setClickable(false); 
		      ibtn_info.setOnClickListener(escucha);
		      
		      ibtn_editar.setFocusable(false);
		      ibtn_editar.setClickable(false); 
		      ibtn_editar.setOnClickListener(escucha);
		      
		    }
		             
		    CasaBean item = items.get(position);
		         
		    ImageView image = (ImageView) vi.findViewById(R.id.imgCasa);
		    int imageResource = activity.getResources().getIdentifier("drawable/"+item.getFoto(), null, activity.getPackageName());
		    image.setImageDrawable(activity.getResources().getDrawable(imageResource));
		    image.setScaleType(ImageView.ScaleType.CENTER);
		    image.setPadding(20, 10,30, 10);
		   
		    txt_nombre = (TextView) vi.findViewById(R.id.txt_nombre_casa);
		    txt_nombre.setText(item.getDescripcion());
		         
		    ibtn_editar.setTag(item);
		    ibtn_info.setTag(item);
		    
		    return vi;
		  }
		  
		 
			private OnClickListener escucha = new OnClickListener()
			{

				@Override
				public void onClick(View v)
				{
					d = (CasaBean)v.getTag();
					
					//Toast.makeText(activity, String.valueOf("idcasa: "+d.getIdCasa()), Toast.LENGTH_SHORT).show();
					
					ImageButton btn=((ImageButton)v);
					
					if(btn.getId()==R.id.ibtnEdit){
						
						//Toast.makeText(activity, String.valueOf("editarr.."), Toast.LENGTH_SHORT).show();
						
						AlertDialog.Builder builder = new AlertDialog.Builder(activity);
					    LayoutInflater inflater = activity.getLayoutInflater();
					    builder.setTitle("Cuadro de Edicion");
					    builder.setView(inflater.inflate(R.layout.dialog_personal, null))
					    // Add action buttons
					           .setPositiveButton("Guardar", new DialogInterface.OnClickListener() {
					               @Override
					               public void onClick(DialogInterface dialog, int id) {
					            	   Dialog diag= (Dialog)dialog;
					            	   EditText nombre_casa= (EditText)diag.findViewById(R.id.txt_edit_nombre);
					            	   String nombre=nombre_casa.getText().toString();
					            	   
					            	   if(ControlHouseON.getInstancia().ActualizarCasa(d.getIdCasa(), nombre, "gohome")){			            	   
					            		   Toast.makeText(activity, String.valueOf("Se actualizo"), Toast.LENGTH_SHORT).show();
					            		   Intent i= new Intent(ListaCasaActivity.this,ListaCasaActivity.class);
					            		   ListaCasaActivity.this.finish();
					            		   startActivity(i);
					            	   }
					            	   else
					            		   Toast.makeText(activity, String.valueOf("Error .."), Toast.LENGTH_SHORT).show();
					               }
					           })
					           .setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
					               public void onClick(DialogInterface dialog, int id) {
					                   dialog.cancel();
					               }
					           });      
					    
					    builder.show();
					
					}
					else {
						
						//Toast.makeText(activity, String.valueOf("infoo.."), Toast.LENGTH_SHORT).show();
						 Intent i = new Intent(ListaCasaActivity.this, InformacionCasaActivity.class);
						 i.putExtra("idCasa",d.getIdCasa());
						 startActivity(i);
						
					}
					
					
				}
			};
	}

}
